<?php
include("../user/userlogincheck.php");
include("../include/config.inc.php");

$code=uuid();

$sql="select * from ".tableprefix."store where member_id='$userid'";
$result=$db->query($sql);
$row = $db->fetch_array($result);
$store_id=$row["store_id"];
if($store_id==''){
	echo("<script type='text/javascript'>location.href='store.php';</script>");
	exit;
}

$todo=$_REQUEST["todo"];
if(isset($todo) && $todo=="todo"){
	$title = $_POST['title'];
	$content = $_POST['content'];
	$code = $_POST['code'];
	
	if($title==""){
		echo "<script>alert('请输入标题！');history.back(-1);</script>";
		exit;
	}
	if($content==""){
		echo "<script>alert('请输入内容！');history.back(-1);</script>";
		exit;
	}
	$sql_img="select * from ".tableprefix."img where code='$code' order by id ASC limit 0,1";
	$res_img=$db->query($sql_img);
	$row_img = $db->fetch_array($res_img);
	$goods_image=$row_img["imgpath"];
	if($goods_image==""){
		echo "<script>alert('请上传缩略图！');history.back(-1);</script>";
		exit;
	}	
	$createtime=date("Y-m-d H:i:s");
	$thumb=$goods_image;
	$state='1';
	$flag='T';
	$sql="insert into ".tableprefix."news(title,content,thumb,userid,code,createtime,state,flag)values('$title','$content','$thumb','$userid','$code','".time()."','$state','$flag')";	
	$db->query($sql);
	if(!$db->error())
	{
		echo "<script>alert('发布成功！');location.href='index.php';</script>";
		$db->close();
		exit;
	}
	else
	{
		echo "<script>alert('数据库出错，请重新发布。');location.href='index.php';</script>";		
		$db->close();
		exit;
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>演艺资讯发布 - 订戏会</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=9,Chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="stylesheet" type="text/css" href="../css/public.css">
<link rel="stylesheet" type="text/css" href="../css/publish.css">
<script language="javascript" type="text/javascript"> 
function showTime(){ 
document.publishForm.content.value=document.getElementById("content").innerHTML;
} 
setInterval("showTime()",1000); 
</script>
</head>

<body>
<div class="head-nav">
	<div class="head-left"><a href="#1"><div class="back">返回</div></a></div>
	<div class="head-center">演艺资讯发布</div>
	<div class="head-right"><a href="#"><div class="shuoming"></div></a></div>
</div>
<div class="remind"><p>发布活动信息、演出资讯、剧目花絮等等。</p></div>

<form action="" method="post" name="publishForm" id="publishForm">
<input name="todo" type="hidden" value="todo">
<input name="code" type="hidden" value="<?php echo $code;?>">
<input name="content" type="hidden" value="">
<div class="mod-form">
	<div class="form-group">
		<div class="form-cell">
			<ul>
				<li><input type="text" id="title" name="title" placeholder="请填写标题" value=""></li>		
			</ul>
		</div>	
	</div>
	<div class="form-group">
		<div class="publish-article-content">
			<div class="title-tips">正文</div>
			<!--<input type="hidden" id="target" name="content">-->
			<div class="content-box"><div contenteditable="true" class="article-content" id="content">请填写内容或上传图片</div></div>
			<!--<div class="footer-btn g-image-upload-box">
				<div class="upload-button">
					<span class="upload"><i class="upload-img"></i>插入图片</span>
					<input class="input-file" id="imageUpload" type="file" name="fileInput" accept="image/*" style="position:absolute;left:0;opacity:0;width:100%;">
				</div>
			</div>-->
		</div>
		<div class="form-img">
			<a class="file" href="javascript:;">
				<span>上传图片</span>
				<input type="file" name="file" id="uploadImg">
			</a>
			<!--<ul id="imgList"></ul>-->
		</div>	
	</div>
	<div class="form-button"><input name="s" type="submit" id="s" value="提&nbsp;&nbsp;交"></div>
	<div class="form-readme"><p>我已阅读并同意<font color="#0099FF">《网演中国服务条款》</font></p></div>
</div>
</form>

<footer id="bar-fixed">
	<a href="../" class="tab-item"><i class="icon-tabbar bar-home"></i>首页</a>
	<a href="../category.php" class="tab-item"><i class="icon-tabbar bar-message"></i>分类</a>
	<a href="../publish/" class="tab-item active"><i class="icon-tabbar bar-publish2"></i>发布</a>	
	<a href="../user/" class="tab-item"><i class="icon-tabbar bar-user"></i>我</a>
</footer>
<script src="../js/frameJS/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/localResizeIMG-master/dist/lrz.bundle.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/layer/layer.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
			$(function() {

				//加载公共头部
				$('header').load('../commonTemplate/common-header.html');
				
				//上传图片
				$('#uploadImg').on('change', function() {
					// this.files[0] 是用户选择的文件
					lrz(this.files[0], {
							width: 1024,
							quality:0.7
					})
					.then(function(rst) {
						// 把处理的好的图片给用户看看呗
						var $imgList = document.getElementById('content');
						var img = new Image();
						img.src = rst.base64;
						//document.write(img.src);
						$.ajax({
         					url: "publish-photoup.php",
         					type: "POST",
         					data:{trans_data:img.src,trans_id:'<?php echo $code;?>'},
         					//dataType: "json",
         					error: function(){  
          					   alert('Error loading XML document');  
        					},  
         					success: function(data,status){//如果调用php成功    
             				//var str = document.getElementById("content").innerHTML + data;
							var str = data;
     						//$("#content").html(str);
							$("#content").append(str);
							//$("#content").appendChild(str);					
             				//alert(unescape(data));//解码，显示汉字
         					}
    					});	 
						img.onload = function() {
							layer.msg('上传中...');
							//$imgList.appendChild(img);
						};
						
						return rst;
					})

					.then(function(rst) {
						
						// 额外添加参数
						rst.formData.append('fileLen', rst.fileLen);
//						$.ajax({
//						    url: 'http://koa-upload.coding.io', // 这个地址做了跨域处理，可以用于实际调试
//						    data: rst.formData,
//						    processData: false,
//						    contentType: false,
//						    type: 'POST',
//						    success: function (data) {
//						        config(JSON.stringify(data));
//						    }
//						});
						return rst;
					})

					.catch(function(err) {
						// 万一出错了，这里可以捕捉到错误信息
						// 而且以上的then都不会执行

						alert(err);
					})

					.always(function() {
						// 不管是成功失败，这里都会执行
					});
				});
			})
		</script>
<script>
if (document.body.clientWidth > 640) {
	$('body').css({
		width: 640,
		margin: '0 auto',
	});
}
</script>
</body>
</html>